package com.irate.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.widget.Toast;

import com.irate.activity.R;
import com.irate.http.AsyncHttpResponseHandler;
import com.irate.http.RequestParams;
import com.irate.model.RatingCriteria;
import com.irate.provider.Constant;
import com.irate.service.AsyncHTTPRest;

public class RatingCriteriaDAO {

	public List<RatingCriteria> parseListData(String json) {

		List<RatingCriteria> listData = new ArrayList<RatingCriteria>();

		try {
			JSONObject obj = new JSONObject(json);
			JSONArray array = obj.getJSONArray("message");

			for (int i = 0; i < array.length(); i++) {
				RatingCriteria criteria = parseData(array.getJSONObject(i));
				listData.add(criteria);
			}

		} catch (Exception e) {

		}

		return listData;
	}

	public RatingCriteria parseData(JSONObject json) {
		RatingCriteria criteria = new RatingCriteria();

		try {

			criteria.setId(json.getString("criteria_id").toString());
			criteria.setName(json.getString("criteria_name").toString());
			criteria.setCat_id(json.getString("category_id").toString());

		} catch (Exception e) {

		}

		return criteria;
	}
	
	public void getListCriteria(AsyncHttpResponseHandler handler,
			Context context, String cate_id, Dialog dialog) {

		ConnectivityManager con = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (con == null || con.getActiveNetworkInfo() == null
				|| !con.getActiveNetworkInfo().isConnected()) {

			Toast.makeText(
					context,
					context.getResources().getString(R.string.connection_error),
					Toast.LENGTH_LONG).show();
			if( dialog!= null){
				dialog.dismiss();
			}
		} else {
			String link = Constant.URL + "/"
					+ Constant.SERVICE_CATEGORY_CRITERIA_RATING + "/"
					+ Constant.SERVICE_GET;

			RequestParams params = new RequestParams();
			params.put("cate_id", cate_id);
			
			Header[] headers = { new BasicHeader("X-API-KEY", Constant.XAPIKEY) };

			AsyncHTTPRest.post(link, headers, params, handler);
		}
	}
	
	public void addCriteria(AsyncHttpResponseHandler handler,
			Context context,String cate_id, String criteria_name, Dialog dialog) {

		ConnectivityManager con = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (con == null || con.getActiveNetworkInfo() == null
				|| !con.getActiveNetworkInfo().isConnected()) {

			Toast.makeText(
					context,
					context.getResources().getString(R.string.connection_error),
					Toast.LENGTH_LONG).show();
			if( dialog!= null){
				dialog.dismiss();
			}
		} else {
			String link = Constant.URL + "/"
					+ Constant.SERVICE_CATEGORY_CRITERIA_RATING + "/"
					+ Constant.SERVICE_ADD;

			RequestParams params = new RequestParams();
			params.put("cate_id", cate_id);
			params.put("criteria_name", criteria_name);
			
			Header[] headers = { new BasicHeader("X-API-KEY", Constant.XAPIKEY) };

			AsyncHTTPRest.post(link, headers, params, handler);
		}
	}
	
}
